<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
    }
    //
    public function index(){
        $data['title']='货品管理 货品 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Product");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }
        $Model->page=$page;
        $Model->perpage=$perpage;
        $product_list=$Model->get_product_list();
        $product_total=$Model->get_product_total();
        //echo $product_total;exit;
        $page_total=ceil($product_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['product_total']=$product_total;
        $pager['page_total']=$page_total;
        $cat_list=$Model->get_cat_list();
        $this->assign("product_list", $product_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //添加产品
    public function product_add(){
        $data['title']='添加产品 产品 - 找金银网';
        $Model=D("Product");
        $cat_list=$Model->get_cat_list();
        //print_r($cat_list);exit;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function product_add_post(){
        if($_POST['product_name']!=''&&$_POST['product_desc']!=''){
            $post=I('post.');
            $post['product_desc']=I('post.product_desc','','');
            $post['product_time']=time();
            //print_r($post);exit;
            $Model=D("Product");
            $Model->post=$post;
            if(!$Model->product_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/product/product_add';</script>";
        }
    }
    //添加货品
    public function product_edit(){
        $data['title']='编辑货品 货品 - 找金银网';
        $product_id=I('path.2');
        $Model=D("Product");
        $City=D("City");
        $Brand=D("Brand");
        $Province=D("Province");
        $Model->product_id=$product_id;
        $product_info=$Model->get_product_info();
        $cat_list=$Model->get_cat_list();
        $city_list=$City->get_city_list();
        $brand_list=$Brand->get_brand_list();
        $province_list=$Province->get_province_list();
        //print_r($city_list);exit;
        $this->assign("data", $data);
        $this->assign("product_info", $product_info);
        $this->assign("cat_list", $cat_list);
        $this->assign("city_list", $city_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("province_list", $province_list);
        $this->display();
    }
    //
    public function product_edit_post(){
        if($_POST['product_name']!=''&&$_POST['product_desc']!=''){
            $post=I('post.');
            if(!empty($_FILES['product_image']['tmp_name'])){
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './data/upload/product/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info   =   $upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $product_image='/data/upload/product/'.$info['product_image']['savename'];
                $post['product_image']=$product_image;
            }
            $post['product_desc']=I('post.product_desc','','');
            //print_r($post);exit;
            $Model=D("Product");
            $Model->post=$post;
            if(!$Model->product_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/product/index';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //更新排序及批量删除
    public function update_product()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Product");
            $Model->ids=$ids;
            if(!$Model->delete_product()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.product_ids')){
            $ids=I('post.product_ids');
            $sorts=I('post.sorts');
            $Model=D("Product");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_product_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //产品分类
    public function cat_list(){
        $data['title']='产品分类列表 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }
    //添加产品分类
    public function cat_add(){
        $data['title']='产品分类添加 - 找金银网';
        $Model=D("ProductClass");
        $top_id_list=$Model->get_top_id_list();
        $parent_id_list=$Model->get_cat_list();
        $this->assign("data", $data);
        $this->assign("top_id_list", $top_id_list);
        $this->assign("parent_id_list", $parent_id_list);
        $this->display();
    }
    //产品分类提交
    public function cat_add_post(){
        $arr=I('post.');
        //$arr['article_content']=I('post.article_content','','');
        $Model=D("ProductClass");
        //print_r($arr);exit;
        if(!$Model->cat_add_post($arr)){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            //返回json
            //echo "{\"statusCode\":\"200\",\"message\":\"操作成功\",\"navTabId\":\"\",\"callbackType\":\"forward\",\"forwardUrl\":\"/article/edit_article/".$arr['article_id']."\"}";
        }
        echo "<script>alert('提交成功');window.location.href='/product/cat_add';</script>";
    }
    //修改产品分类
    public function cat_edit(){
        $data['title']='产品分类修改 - 找金银网';
        $pc_id=I('path.2');
        $Model=D("ProductClass");
        $Model->pc_id=$pc_id;
        $info=$Model->get_cat_info();
        $top_id_list=$Model->get_top_id_list();
        $parent_id_list=$Model->get_cat_list();
        $this->assign("data", $data);
        $this->assign("info", $info);
        $this->assign("top_id_list", $top_id_list);
        $this->assign("parent_id_list", $parent_id_list);
        $this->display();
    }
    //修改产品分类提交
    public function cat_edit_post(){
        $post=I('post.');
        $Model=D("ProductClass");
        $Model->post=$post;
        if(!$Model->cat_edit_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/product/cat_list';</script>";
    }
    //删除
    public function delete_cat()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("ProductClass");
            $Model->ids=$ids;
            if(!$Model->delete_cat()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.pc_ids')){
            $pc_ids=I('post.pc_ids');
            $sorts=I('post.sorts');
            //print_r($sorts);exit;
            $Model=D("ProductClass");
            $Model->ids=$pc_ids;
            $Model->sorts=$sorts;
            if(!$Model->update_sorts()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function purchase(){
        $data['title']='采购管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }
    //
    public function quote(){
        $data['title']='报价管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }

    //品牌管理
    public function brand(){
        $data['title']='品牌管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("Brand");

        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }

        $Model->page=$page;
        $Model->perpage=$perpage;
        $brand_list=$Model->get_brand_list();
        //分页相关
        $brand_total=$Model->get_brand_total();
        $page_total=ceil($brand_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$brand_total;

        //dump($page_total);
        // exit();
        $this->assign("data", $data);
        $this->assign("pager", $pager);
        $this->assign("brand_list", $brand_list);
        $this->display();
    }

    public function  brand_add(){

        $data['title']='品牌添加 货品 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }

    public function  brand_edit(){
        $Model=D("Brand");
        $Model->brand_id=I('path.2');
        //echo I('path.2');exit;
        $brand=$Model->get_brand();
        $data['title']='品牌编辑 货品 - 找金银网';
        $this->assign("data", $data);
        $this->assign("content", $brand);
        $this->display();
    }

    public function brand_add_post(){
        $post=I('post.');
        $brandId=I('path.2');

        $Model=D("Brand");
        $Model->post=$post;
        $Model->brandId=$brandId;

        if($Model->brand_add_post()  &&  $post["brand_name"]){
            echo "<script>alert('提交成功');window.location.href='/product/brand';</script>";exit;
        }
        echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";
    }

    public function update_brand()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Brand");
            $Model->ids=$ids;
            if(!$Model->delete_brand()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.brand_ids')){
            $ids=I('post.brand_ids');
            $sorts=I('post.sorts');
            $Model=D("Brand");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_brand_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }

    public function depot(){
        $data['title']='仓库管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }

        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("Depot");

        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }

        $Model->page=$page;
        $Model->perpage=$perpage;
        $depot_list=$Model->get_depot_list();
        //分页相关
        $depot_total=$Model->get_depot_total();
        $page_total=ceil($depot_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$depot_total;

        $this->assign("data", $data);
        $this->assign("pager", $pager);
        $this->assign("depot_list", $depot_list);
        $this->display();
    }

    public function  depot_add(){

        $data['title']='品牌添加 货品 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }

    public function depot_add_post(){
        $post=I('post.');
        $depot_id=I('path.2');

        $Model=D("Depot");
        $Model->post=$post;
        $Model->$depot_id=$depot_id;

        if($Model->depot_add_post()  &&  $post["depot_name"]){
            echo "<script>alert('提交成功');window.location.href='/product/depot';</script>";exit;
        }
        echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";
    }

    public function  depot_edit(){

        $Model=D("Depot");
        $Model->$depot_id=I('path.2');
        $depot=$Model->get_depot();
        $data['title']='品牌编辑 货品 - 找金银网';
        $this->assign("data", $data);
        $this->assign("info", $depot);
        $this->display();
    }

    public function update_depot()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("depot");
            $Model->ids=$ids;
            if(!$Model->delete_depot()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.depot_ids')){
            $ids=I('post.depot_ids');
            $sorts=I('post.sorts');
            $Model=D("Depot");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_depot_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }

    //
    public function type(){
        $data['title']='类型管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }
    //
    public function specifications(){
        $data['title']='规格管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }
    //
    public function image(){
        $data['title']='图片管理 货品 - 找金银网';
        //获取产品分类列表
        if(I('path.2')>0){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        //echo $page.'<br />'.$perpage;exit;
        $Model=D("ProductClass");
        $Model->page=$page;
        $Model->perpage=$perpage;
        $cat_list=$Model->get_cat_list();
        //分页相关
        $cat_total=$Model->get_cat_total();
        $page_total=round($cat_total/$perpage);
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        $pager['cat_total']=$cat_total;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager", $pager);
        $this->display();
    }
    public function purchase_edit(){
            
        $this->display();
    }
}